package com.qiantu.phone.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hjq.base.BaseDialog;
import com.hjq.http.lifecycle.ApplicationLifecycle;
import com.hjq.http.listener.HttpCallback;
import com.qiantu.api.entity.VersionBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.BuildConfig;
import com.qiantu.phone.R;
import com.qiantu.phone.aop.Log;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.ui.dialog.ShareBottomDialog;
import com.qiantu.phone.ui.dialog.UpdateDialog;
import com.qiantu.phone.utils.Constants;

/**
 * author : Android 轮子哥
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2018/10/18
 */
public final class AboutAppActivity extends AppActivity {

    @Log
    public static void start(Context context) {
        Intent intent = new Intent(context, AboutAppActivity.class);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    private LinearLayout versionLayout;
    private TextView tvVersion;
    private ShareBottomDialog mShareBottomDialog;
    private BaseDialog mUpdateDialog;
    private VersionBean mVersionBean;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_about_app;
    }

    @Override
    protected void initView() {
        versionLayout = findViewById(R.id.btn_check_update);
        tvVersion = findViewById(R.id.tv_version);
        tvVersion.setText(getString(R.string.version_holder, BuildConfig.VERSION_NAME));
//        if(AppApplication.isHuawei()){
            versionLayout.setVisibility(View.GONE);
//        }
        setOnClickListener(R.id.btn_check_update, R.id.btn_privacy_policy, R.id.btn_user_agreement, R.id.btn_share);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_check_update:
                checkUpdate();
                break;
            case R.id.btn_privacy_policy:
                BrowserActivity.start(this, "https://sv-center.qiantu-iot.com/privacy/privacy-policy.html");
                break;
            case R.id.btn_user_agreement:
                BrowserActivity.start(this, "https://sv-center.qiantu-iot.com/privacy/index_boyun.html");
                break;
            case R.id.btn_share:
                showShareBottomDialog();
                break;
        }
    }

    private void checkUpdate(){
        LLHttpManager.checkUpdate(new ApplicationLifecycle(), BuildConfig.CLIENT_CODE, new HttpCallback<HttpData<VersionBean>>(this) {
            @Override
            public void onSucceed(HttpData<VersionBean> result) {
                super.onSucceed(result);
                mVersionBean = result.getData();
                if(mVersionBean != null){
                    showUpdateDialog();
                }else{
                    toast(R.string.newest_version_hint);
                }
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }
        });
    }

    /**
     * 版本号必须是两个点以上
     *
     * @param currentVersion 当前版本号
     * @param upgradeVersion 最新版本号
     * @return
     */
    private boolean compareVersion(String[] currentVersion, String[] upgradeVersion) {
        int size = Math.max(currentVersion.length, upgradeVersion.length);
        String[] currentVersionNew = new String[size];
        String[] upgradeVersionNew = new String[size];
        for (int i = 0; i < size; i++) {
            if (i < currentVersion.length) {
                currentVersionNew[i] = currentVersion[i];
            } else {
                currentVersionNew[i] = "0";
            }
            if (i < upgradeVersion.length) {
                upgradeVersionNew[i] = upgradeVersion[i];
            } else {
                upgradeVersionNew[i] = "0";
            }
        }
        if (size < 3)
            return false;
        for (int i = 0; i < size; i++) {
            int currentVersionNum = Integer.parseInt(currentVersionNew[i]);
            int upgradeVersionNum = Integer.parseInt(upgradeVersionNew[i]);
            if (upgradeVersionNum < currentVersionNum)
                return false;
            if (upgradeVersionNum > currentVersionNum)
                return true;
        }
        //相等
        return false;
    }

    private UpdateDialog.Builder mUpdateDialogBuilder;
    private void showUpdateDialog(){
        if(TextUtils.isEmpty(mVersionBean.getLatestVersion())){
            return;
        }
        String[] currentVersion = BuildConfig.VERSION_NAME.split("\\.");
        String[] upgradeVersion = mVersionBean.getLatestVersion().split("\\.");
        if(!compareVersion(currentVersion, upgradeVersion)){
            toast(R.string.newest_version_hint);
            return;
        }
        if(mUpdateDialog == null){
            mUpdateDialogBuilder = new UpdateDialog.Builder(getActivity());
            mUpdateDialog = mUpdateDialogBuilder.create();
        }
        mUpdateDialogBuilder.setVersionName(mVersionBean.getLatestVersion());
        mUpdateDialogBuilder.setDownloadUrl(mVersionBean.getPackageUrl());
        mUpdateDialogBuilder.setUpdateLog(mVersionBean.getUpdateContent());
        mUpdateDialog.addOnShowListener(new BaseDialog.OnShowListener() {
            @Override
            public void onShow(BaseDialog dialog) {
                mUpdateDialogBuilder.refreshUpdateLogLayout();
            }
        });
        mUpdateDialog.show();
    }

    /**
     * 选择图片
     */
    private void showShareBottomDialog(){
        if(mShareBottomDialog == null){
            mShareBottomDialog = new ShareBottomDialog(this, R.style.ActionSheetDialogStyle);
            mShareBottomDialog.setOnItemClickListener(new ShareBottomDialog.OnItemClickListener() {
                @Override
                public void onItemClick(int type) {

                }
            });
        }
        mShareBottomDialog.show();
    }
}